package com.constant;

import com.annotations.rpc.Discovery;
import com.config.Config;
import com.utils.ClassScan;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author linyu.dai
 * @project rpc
 * @description 服务端业务Service扫描
 * @creat 2022/11/27 22:04:05
 */
@Slf4j
public class ServiceConstant {


    public static final String SCAN_PATH = Config.get("path.scan");

    private static final Map<String, String> SERVICE_MAP;

    static {
        SERVICE_MAP = ClassScan.scan(SCAN_PATH).stream()
                .filter(path -> Objects.nonNull(ClassScan.getAnnotation(path, Discovery.class)))
                .collect(Collectors.toMap(path -> {
                    Discovery annotation = ClassScan.getAnnotation(path, Discovery.class);
                    return annotation.name();
                }, (path) -> path));
    }

    public static Class<?> getClassPath(String serviceName) throws ClassNotFoundException {
           return Class.forName(SERVICE_MAP.get(serviceName));
    }



    /**
     * @Author linyu.dai
     * @Description 获取服务 文件映射
     * @Date 2022/11/27 20:50
     * @Param []
     * @return java.util.Map<java.lang.String,java.lang.String>
     */
    private Map<String, String> getService(){
        return ClassScan.scan(SCAN_PATH).stream()
                .filter(path -> Objects.nonNull(ClassScan.getAnnotation(path, Discovery.class)))
                .collect(Collectors.toMap(path -> {
                    Discovery annotation = ClassScan.getAnnotation(path, Discovery.class);
                    return annotation.name();
                }, (path) -> path));
    }
}
